#!coding:utf-8
from celery import current_task
from celery import task
from djcelery.models import PeriodicTask, TaskMeta
from cvirt.common.constant import TaskAction
from cvirt.common import utils
from cvirt.tasks import TaskWrap, link_sync_call
from cvirt.manager.models import Network, TaskDetail
import  logging

LOG = logging.getLogger("task.network")

api = utils.cnode_import_class('cvirt.cnode.network.api')

#初始化已经存在的网络
if api: api.init_networks()

def create(network,apifunc,task,uuid,*args):
    if network:
        try:
            current_task.update_state(state="building")
            if TaskDetail.create_if_not_exsist(uuid,name=TaskAction.CREATE_NETWORK,type=TaskAction.NETWORK,
                                               target=network.name,meta_id=task.request.id):
                #初始化fixedip是一次性任务，在第一个节点做
                if network.networktype in [2,3]:
                    api.init_fixedips(network)

            network_id = apifunc(network,*args)
            return network_id
        except:
            LOG.info('create network failed,delete network_ref')
            api.delete_network(network)
            network.delete()
            raise


def update(network,apifunc,task,uuid,last_cidr=None,*args):
    if network:
        try:
            current_task.update_state(state="updating")
            if TaskDetail.create_if_not_exsist(uuid,name=TaskAction.UPDATE_NETWORK,type=TaskAction.NETWORK,
                                               target=network.name,meta_id=task.request.id):
                #初始化fixedip是一次性任务，在第一个节点做
                if network.networktype in [2,3]:
                    api.remove_fixedips(network)
                    api.init_fixedips(network)

            if network.networktype in [2,3]:
                network_id = apifunc(network,last_cidr=last_cidr,*args)
            else:
                network_id = apifunc(network,*args)
            return network_id
        except:
            LOG.info('update network failed')
            raise



@TaskWrap
@link_sync_call
@task()
def createNat(network,uuid=None):

    create(network,api.create_nat_network,createNat.task.task,uuid)

@TaskWrap
@link_sync_call
@task()
def createIsolate(network,uuid=None):
    create(network,api.create_isolate_network,createIsolate.task.task,uuid)


@TaskWrap
@link_sync_call
@task()
def updateNat(network,uuid=None):

    update(network,api.update_nat_network,updateNat.task.task,uuid)

@TaskWrap
@link_sync_call
@task()
def updateIsolate(network,uuid=None):
    update(network,api.update_isolate_network,updateIsolate.task.task,uuid)


@TaskWrap
@link_sync_call
@task()
def createBridge(network,uuid=None):
    create(network,api.create_bridge_network,createBridge.task.task,uuid)

@TaskWrap
@link_sync_call
@task()
def createVlan(network,uuid=None):
    create(network,api.create_vlan_network,createVlan.task.task,uuid)


@TaskWrap
@link_sync_call
@task()
def delete_network(network,uuid=None):

    try:
        current_task.update_state(state="removing")

        first = TaskDetail.create_if_not_exsist(uuid,name=TaskAction.DELETE_NETWORK,type=TaskAction.NETWORK,
                              target=network.name,meta_id=delete_network.task.task.request.id)

        api.delete_network(network)
        if first:
            network.remove()

    except:
        LOG.info('delete network failed,delete network_ref')
        raise



@TaskWrap
@link_sync_call
@task()
def updateVlan(network,uuid=None,last_cidr=None):
    update(network,api.update_vlan_network,updateVlan.task.task,uuid,last_cidr=last_cidr)



@TaskWrap
@link_sync_call
@task()
def updateBridge(network,uuid=None,last_cidr=None):
    update(network,api.update_bridge_network,updateBridge.task.task,uuid,last_cidr=last_cidr)


